/**
 * 文件名：****.java
 * 版权：Copyright Lightkit All Rights Reserved.
 * 描述：*****
 */
package com.lightkits.wms.domain.usuallyinterface;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lightkits.common.TransactionValidate;
import com.lightkits.common.db.entity.FndOrganizationTl;
import com.lightkits.common.db.entity.FndWarehouse;
import com.lightkits.common.db.entity.FndWmArea;
import com.lightkits.framework.common.logging.Logger;
import com.lightkits.framework.common.logging.LoggerFactory;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.framework.soa.hzero.executor.ServiceExecutor;
import com.lightkits.wms.dto.usuallyinterface.GetWmAreaRequestType;
import com.lightkits.wms.dto.usuallyinterface.GetWmAreaResponseType;
import com.lightkits.wms.dto.usuallyinterface.model.WmAreaDTO;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取货位详情
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/18 16:03
 */
@Component
public class GetWmAreaExecutor implements ServiceExecutor<GetWmAreaRequestType, GetWmAreaResponseType> {
    private static final Logger LOGGER = LoggerFactory.getLogger(GetWmMoveTypeExecutor.class);

    @Override
    public GetWmAreaResponseType service(GetWmAreaRequestType request) {
        GetWmAreaResponseType response = new GetWmAreaResponseType();
        response.setSuccessful(true);
        try {
            TransactionValidate check1 = TransactionValidate.beginValidity(FndWarehouse.class);
            check1.checkIdValidity(ObjectUtil.toString(request.getWarehouseId()), "ID");
            List<FndWmArea> fndWmAreas = new FndWmArea().
                    selectList(new EntityWrapper<FndWmArea>()
                            .eq(FndWmArea.WAREHOUSE_ID, request.getWarehouseId())
                    );
            if (ObjectUtil.isNullOrEmpty(fndWmAreas)) {
                response.setWmAreaDTOS(new ArrayList<>());
                return response;
            }
            response.setWmAreaDTOS(convertDto(fndWmAreas));
        } catch (Exception e) {
            response.setSuccessful(false);
            response.setMessage("get WM Area error");
        }
        return response;
    }

    /**
     * 货位类型转换
     *
     * @param fndWmAreas 参数货位
     * @return WmAreaDTO
     **/
    private List<WmAreaDTO> convertDto(List<FndWmArea> fndWmAreas) {
        List<WmAreaDTO> dtos = new ArrayList<WmAreaDTO>();
        try {
            fndWmAreas.forEach(target -> {
                WmAreaDTO areaDTO = new WmAreaDTO();
                areaDTO.setWarehouseId(target.getWarehouseId().toString());
                areaDTO.setWmAreaId(target.getId().toString());
                areaDTO.setWmAreaCode(target.getWmAreaCode());
                //获取货位名称
                FndOrganizationTl tl = new FndOrganizationTl().selectOne(new EntityWrapper()
                        .eq(FndOrganizationTl.ORGANIZATION_ID, target.getId())
                );
                if (!ObjectUtil.isEmpty(tl)) {
                    areaDTO.setWmAreaName(tl.getOrganizationName());
                }
                dtos.add(areaDTO);
            });
        } catch (Exception e) {
            LOGGER.error("convertDto.error", e);
        }
        return dtos;
    }
}
